//
//  SXAllReadListTableViewController.m
//  ShiXun
//
//  Created by lanou on 15/10/29.
//  Copyright © 2015年 张琪. All rights reserved.
//

#import "SXAllReadListTableViewController.h"
#import "SXRequestManager.h"
#import "SXReadListModel.h"
#import "SXAllReadListTableViewCell.h"
#import "SXRefreshView.h"

@interface SXAllReadListTableViewController ()

@property (nonatomic, strong) NSMutableArray *dataSourceArray;

@property (nonatomic, strong) SXRefreshView *refreshingView;

@end

@implementation SXAllReadListTableViewController

- (NSMutableArray *)dataSourceArray
{
    if (!_dataSourceArray) {
        _dataSourceArray = [NSMutableArray array];
    }
    return _dataSourceArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBar.hidden = NO;
    self.navigationItem.title = @"全部阅读";
    
    [self.tableView registerClass:[SXAllReadListTableViewCell class] forCellReuseIdentifier:@"SXAllReadListTableViewCell"];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    
    [self requestData];
    
    _refreshingView = [[SXRefreshView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:_refreshingView];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

// 网络请求
- (void)requestData
{
    [SXRequestManager requestWithType:GET urlString:kReadAllListURLString parDic:nil finish:^(NSData *data) {
        NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableLeaves |NSJSONReadingMutableContainers) error:nil];
        for (NSDictionary *dic in array) {
            SXReadListModel *model = [[SXReadListModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataSourceArray addObject:model];
        }
        [_refreshingView removeFromSuperview];
        [self.tableView reloadData];
    } error:^(NSError *error) {
        NSLog(@"%@", error);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSourceArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    SXReadListModel *model = self.dataSourceArray[section];
    if (model.isOpen) {
        return 1;
    }
    return 0;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SXAllReadListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SXAllReadListTableViewCell" forIndexPath:indexPath];
    
    // Configure the cell...
    
    cell.allReadVC = self;
    cell.configureArr = [self.dataSourceArray[indexPath.section] songsArray];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.dataSourceArray[indexPath.section] songsArray].count % 5 == 0) {
        return [self.dataSourceArray[indexPath.section] songsArray].count / 5 * 63;
    }
    else
    {
        return [self.dataSourceArray[indexPath.section] songsArray].count / 5 * 63 + 63;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}

// 添加分组头
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 40)];
    headerView.backgroundColor = [UIColor whiteColor];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, kScreenWidth, 40)];
    SXReadListModel *model = self.dataSourceArray[section];
    titleLabel.font = [UIFont systemFontOfSize:16];
    titleLabel.text = model.cName;
    [headerView addSubview:titleLabel];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 10, 20, 20)];
    if (model.isOpen) {
        imageView.image = [UIImage imageNamed:@"SX-xiasanjiao"];
    }
    else{
        imageView.image = [UIImage imageNamed:@"SX-yousanjiao"];
    }
    [headerView addSubview:imageView];
    
    UIButton *button = [UIButton buttonWithType:(UIButtonTypeSystem)];
    button.frame = headerView.bounds;
    button.tag = 1000 + section;
    [button addTarget:self action:@selector(changeRow:) forControlEvents:(UIControlEventTouchUpInside)];
    [headerView addSubview:button];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(10, 39, kScreenWidth - 20, 1)];
    lineView.backgroundColor = [UIColor colorWithWhite:0.631 alpha:1.000];
    [headerView addSubview:lineView];
    
    return headerView;
}

- (void)changeRow:(UIButton *)button
{
    NSInteger selectSection = button.tag - 1000;
    SXReadListModel *model = self.dataSourceArray[selectSection];
    model.isOpen = !model.isOpen;
    
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:selectSection] withRowAnimation:(UITableViewRowAnimationFade)];
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
